package com.fieldchain.ex.fa100.ui.access;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseFragment;
import com.fieldchain.ex.fa100.bean.CoinSufBean;
import com.fieldchain.ex.fa100.bean.event.TradEvent;
import com.fieldchain.ex.fa100.ui.personal.activity.PersonalCanterActivity;
import com.fieldchain.ex.fa100.ui.trade.activity.ChoseMarketActivity;
import com.fieldchain.ex.fa100.ui.trade.fragment.BaseTradFragment;
import com.fieldchain.ex.fa100.ui.trade.fragment.TradBuyFragment;
import com.fieldchain.ex.fa100.ui.trade.fragment.TradEntiustFragment;
import com.fieldchain.ex.fa100.ui.trade.fragment.TradHistoryFragment;
import com.fieldchain.ex.fa100.ui.trade.fragment.TradSellFragment;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequest;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/16
 */
public class TradFragment extends MyBaseFragment {
    private ImageView ivPersonal;
    private LinearLayout linTop;
    private TextView tvTitle;
    private RadioButton rb0;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private ImageView ivSlice;

    private int index, oldindex;
    private List<CoinSufBean> mList = new ArrayList<>();
    private BaseTradFragment[] fragments = new BaseTradFragment[4];
    private String market;
    private boolean needRemote = true;
    private int pageTag = 0;

    @Override
    protected void findView() {
        ivPersonal = view.findViewById(R.id.iv_personal);
        linTop = view.findViewById(R.id.lin_top);
        tvTitle = view.findViewById(R.id.tv_title);
        rb0 = view.findViewById(R.id.rb0);
        rb1 = view.findViewById(R.id.rb1);
        rb2 = view.findViewById(R.id.rb2);
        rb3 = view.findViewById(R.id.rb3);
        ivSlice = view.findViewById(R.id.iv_slice);
    }

    @Override
    protected void setValue() {

        fragments[0] = new TradBuyFragment();
        fragments[1] = new TradSellFragment();
        fragments[2] = new TradEntiustFragment();
        fragments[3] = new TradHistoryFragment();

        setListener(rb0, 0);
        setListener(rb1, 1);
        setListener(rb2, 2);
        setListener(rb3, 3);

        FragmentTransaction transaction_home = getChildFragmentManager().beginTransaction();
        transaction_home.add(R.id.frm, fragments[0]);
        transaction_home.commit();


        linTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mList.size() == 0) {
                    getCoin();
//                    MyToast.show("数据获取中，请稍等");
                    return;
                }
                Intent intent = new Intent(mContext, ChoseMarketActivity.class);
                intent.putExtra("list", (Serializable) mList);
                intent.putExtra("market", market);
                intent.putExtra("page",index);
                startActivity(intent);
            }
        });

        ivPersonal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, PersonalCanterActivity.class));
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (index !=pageTag ) {
            index=pageTag;
            ((RadioButton) ((RadioGroup)(rb0.getParent())).getChildAt(index)).setChecked(true);
        }
        if(needRemote){
            if(mList.size()==0) {
                getCoin();
            }else {
                refreshChild();
                if (!tvTitle.getText().toString().equals(market.toUpperCase())) {
                    tvTitle.setText(market.toUpperCase());
                }
            }
        }else {
            if (!tvTitle.getText().toString().equals(market.toUpperCase())) {
                tvTitle.setText(market.toUpperCase());
            }
            refreshChild();
        }
        isFirst = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e("trad:", "onPause");
        fragments[index].onPause();
        needRemote = true;
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_trade;
    }

    public void setListener(RadioButton rb, final int tag) {
        rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    index = tag;
                    switchFragment();
                }
            }
        });
    }

    //正确的做法
    private void switchFragment() {
        Bundle args = new Bundle();
        args.putString("market", market);
        fragments[index].setArguments(args);

        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.frm, fragments[index]).commit();
        oldindex = index;
    }



    //外部来的market,在onResum 触发 refreshChild()
    public void refresh(String mk, int page) {
        needRemote = false;
        this.pageTag = page;
        market = mk;
    }

    //通知子fragment ,修改title
    private void refreshChild() {
        String mk = market;
        mk = mk.replace("_", "/");
        tvTitle.setText(mk.toUpperCase());

        if(market.length()>0&&market.contains("_"))
        fragments[index].setMarket(market);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void TradEvent(TradEvent et) {
        if (et.getType() == 2) {
            rb2.setChecked(true);
        }
    }

    @Override
    public void loginShow() {

    }
    public void getCoin() {
        HttpRequest.getInstance().getRequest(Constant.trad_coin, new ListRequest<CoinSufBean>() {
            @Override
            public void onSucess(List<CoinSufBean> list, int code, String msg) {
                if (code == 200) {
                    if(list.size()>0) {
                        mList.clear();
                        mList.addAll(list);
                        market = list.get(0).getListMarket().get(0);
                        if(needRemote){
                            if (!tvTitle.getText().toString().equals(market.toUpperCase())) {
                                tvTitle.setText(market.toUpperCase());
                            }
                            refreshChild();
                        }
                    }
                }

            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
